﻿using System;
using UnityEngine;

// Token: 0x0200014D RID: 333
[DisallowMultipleComponent]
[AddComponentMenu("RVP/C#/Ground Surface/Terrain Surface", 2)]
[RequireComponent(typeof(Terrain))]
[ExecuteInEditMode]
public class TerrainSurface : MonoBehaviour
{
	// Token: 0x060005FB RID: 1531 RVA: 0x00030958 File Offset: 0x0002EB58
	private void Start()
	{
		this.tr = base.transform;
		if (base.GetComponent<Terrain>().terrainData)
		{
			this.terDat = base.GetComponent<Terrain>().terrainData;
			if (Application.isPlaying)
			{
				this.UpdateAlphamaps();
				this.frictions = new float[this.surfaceTypes.Length];
				for (int i = 0; i < this.frictions.Length; i++)
				{
					if (GroundSurfaceMaster.surfaceTypesStatic[this.surfaceTypes[i]].useColliderFriction)
					{
						this.frictions[i] = base.GetComponent<Collider>().material.dynamicFriction * 2f;
					}
					else
					{
						this.frictions[i] = GroundSurfaceMaster.surfaceTypesStatic[this.surfaceTypes[i]].friction;
					}
				}
			}
		}
	}

	// Token: 0x060005FC RID: 1532 RVA: 0x00030A29 File Offset: 0x0002EC29
	private void Update()
	{
		if (!Application.isPlaying && this.terDat && this.surfaceTypes.Length != this.terDat.alphamapLayers)
		{
			this.ChangeSurfaceTypesLength();
		}
	}

	// Token: 0x060005FD RID: 1533 RVA: 0x00030A63 File Offset: 0x0002EC63
	public void UpdateAlphamaps()
	{
		this.terrainAlphamap = this.terDat.GetAlphamaps(0, 0, this.terDat.alphamapWidth, this.terDat.alphamapHeight);
	}

	// Token: 0x060005FE RID: 1534 RVA: 0x00030A90 File Offset: 0x0002EC90
	private void ChangeSurfaceTypesLength()
	{
		int[] array = this.surfaceTypes;
		this.surfaceTypes = new int[this.terDat.alphamapLayers];
		for (int i = 0; i < this.surfaceTypes.Length; i++)
		{
			if (i >= array.Length)
			{
				break;
			}
			this.surfaceTypes[i] = array[i];
		}
	}

	// Token: 0x060005FF RID: 1535 RVA: 0x00030AEC File Offset: 0x0002ECEC
	public int GetDominantSurfaceTypeAtPoint(Vector3 pos)
	{
		Vector2 vector = new Vector2(Mathf.Clamp01((pos.z - this.tr.position.z) / this.terDat.size.z), Mathf.Clamp01((pos.x - this.tr.position.x) / this.terDat.size.x));
		float num = 0f;
		int num2 = 0;
		for (int i = 0; i < this.terrainAlphamap.GetLength(2); i++)
		{
			float num3 = this.terrainAlphamap[Mathf.FloorToInt(vector.x * (float)(this.terDat.alphamapWidth - 1)), Mathf.FloorToInt(vector.y * (float)(this.terDat.alphamapHeight - 1)), i];
			if (num3 > num)
			{
				num = num3;
				num2 = i;
			}
		}
		return this.surfaceTypes[num2];
	}

	// Token: 0x06000600 RID: 1536 RVA: 0x00030BFC File Offset: 0x0002EDFC
	public float GetFriction(int sType)
	{
		float result = 1f;
		for (int i = 0; i < this.surfaceTypes.Length; i++)
		{
			if (sType == this.surfaceTypes[i])
			{
				result = this.frictions[i];
				break;
			}
		}
		return result;
	}

	// Token: 0x04000936 RID: 2358
	private Transform tr;

	// Token: 0x04000937 RID: 2359
	private TerrainData terDat;

	// Token: 0x04000938 RID: 2360
	private float[,,] terrainAlphamap;

	// Token: 0x04000939 RID: 2361
	public int[] surfaceTypes = new int[0];

	// Token: 0x0400093A RID: 2362
	[NonSerialized]
	public float[] frictions;
}
